package com.elvin.platform.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.elvin.platform.beans.Organize;
import com.elvin.platform.mapper.OrganizeMapper;
import com.elvin.platform.service.OrganizeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;


@Service
public class OrganizeServiceImpl extends ServiceImpl<OrganizeMapper, Organize> implements OrganizeService{
	@Resource
	private OrganizeMapper mapper;

	@Override
	@Transactional
	public void insertOrganization(Organize entity) {
		//新增的时候需要重新编写组织机构编
		mapper.insert(entity);
		//获取父id
		String parentIdStr = mapper.selectSelfAndParentId(entity.getId());
		String[] split = parentIdStr.split(",");
		StringBuffer stringBuffer = new StringBuffer();
		for (int i = split.length-1; i>=0; i--) {
			stringBuffer.append(split[i]);
			if(i !=0){
				stringBuffer.append("-");
			}
		}
		String code = stringBuffer.toString();
		entity.setCode(code);
		mapper.updateById(entity);
	}

	@Override
	public void deleteSelfAndChild(Long id) {
		String ids = mapper.selectSelfAndChildId(id);
		mapper.deleteSelfAndChildByIds(ids);
	}

	@Override
	public List<Organize> selectSelfAndChild(HashMap<String, Object> map) {
		String ids = mapper.selectSelfAndChildId((Long) map.get("id"));
		map.put("ids",ids);
		return mapper.selectSelfAndChild(map);
	}

  @Override
  public void updateDictionaryInfo(Organize entity) {
     mapper.updateDictionaryInfo(entity);
  }

}
